import Vue from 'vue'
import VueRouter from 'vue-router'
//使用路由插件
Vue.use(VueRouter)
//引入路由组件
import Home from '@/pages/Home'
import Login from '@/pages/Login'
import Search from '@/pages/Search'
import Register from '@/pages/Register'
//先把VueRouter在原型对象的push，保存一份
let originPush = VueRouter.prototype.push;
let originReplace = VueRouter.prototype.replace
//重写push和replace
VueRouter.prototype.push = function (location, resolve, reject) {
    if (resolve && reject) {
        //call和apply都可以修改函数的上下文
        originPush.call(this, location, resolve, reject)
    } else {
        originPush.call(this, location, () => { }, () => { })
    }
}

VueRouter.prototype.replace = function (location, resolve, reject) {
    if (resolve && reject) {
        originReplace.call(this, location, resolve, reject)
    } else {
        originReplace.call(this, location, () => { }, () => { })
    }
}
//配置路由
export default new VueRouter({
    routes: [
        {
            path: '/home',
            component: Home,
            meta: {
                isShow: true
            }
        },
        {
            path: '/login',
            component: Login,
            meta: {
                isShow: false
            }
        },
        {
            name: 'search',
            // path: '/search',
            path: '/search/:keyWord?',
            component: Search,
            meta: {
                isShow: true
            }
        },
        {
            path: '/register',
            component: Register,
            meta: {
                isShow: false
            }
        },
        {
            //重定向，让项目起始为home
            path: '*',
            redirect: '/home'
        }

    ]
})